/**
 * stc89c52成功
 * */
#include <reg52.h>
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit scl = P3 ^ 6;
sbit sda = P3 ^ 7;
char ADDR = 0x4E; // PCF8574  T  模块的地址码
void delay()      //
{
        ;
        ;
}

void init() //IIC初始化
{
        sda = 1;
        delay();
        scl = 1;
        delay();
}
void start() //IIC开始信号
{
        sda = 1;
        delay();
        scl = 1;
        delay();
        sda = 0;
        delay();
}

void stop() //IIC停止
{
        sda = 0;
        delay();
        scl = 1;
        delay();
        sda = 1;
        delay();
}

void respons() //IIC应答
{
        uchar i;
        scl = 1;
        delay();
        while ((sda == 1) && (i < 250))
                i++;
        scl = 0;
        delay();
}

void write_byte(uchar date) //IIC写入数据
{
        uchar i, temp;
        temp = date;

        for (i = 0; i < 8; i++)
        {
                temp = temp << 1;
                scl = 0;
                delay();
                sda = CY;
                delay();
                scl = 1;
                delay();
        }
        scl = 0;
        delay();
        sda = 1;
        delay();
}

uchar read_byte() //IIC读取字节
{
        uchar i, k;
        scl = 0;
        delay();
        sda = 1;
        delay();
        for (i = 0; i < 8; i++)
        {
                scl = 1;
                delay();
                k = (k << 1) | sda;
                scl = 0;
                delay();
        }
        return k;
}

void write_add(uchar date1) //写入数据到IO
{
        start();
        write_byte(ADDR);
        respons();
        write_byte(date1);
        respons();
        stop();
}

uchar read_add() //向IO读取数据
{
        uchar date1;
        start();
        write_byte(0x71);
        respons();
        date1 = read_byte();
        respons();
        stop();
        return date1;
}
void delay1(uchar x)
{
        uchar a, b;
        for (a = x; a > 0; a--)
                for (b = 200; b > 0; b--)
                        ;
}

void write_com(uchar com) //写命令函数
{
        uchar com1, com2;
        com1 = com | 0x0f;
        write_add(com1 & 0xfc);
        delay1(2);
        write_add(com1 & 0xf8);
        com2 = com << 4;
        com2 = com2 | 0x0f;
        write_add(com2 & 0xfc);
        delay1(2);
        write_add(com2 & 0xf8);
}

void write_date(uchar date) //写数据函数
{
        uchar date1, date2;
        date1 = date | 0x0f;
        write_add(date1 & 0xfd);
        delay1(2);
        write_add(date1 & 0xf9);
        date2 = date << 4;
        date2 = date2 | 0x0f;
        write_add(date2 & 0xfd);
        delay1(2);
        write_add(date2 & 0xf9);
}

void init_lcd() //初始化函数
{
        write_com(0x33); //显示模式设置
        delay1(6);
        write_com(0x32); //显示模式设置
        delay1(6);
        write_com(0x28); //4位总线，双行显示，显示5×7的点阵字符
        delay1(6);
        write_com(0x01); //清屏
        delay1(6);
        write_com(0x06); //字符进入模式：屏幕不动，字符后移
        delay1(6);
        write_com(0x0c); //显示开，关光标
        //write_LCD_Command(0x0f); //显示开,开光标,光标闪烁
        delay1(6);
}

//显示字符串：第x行第y列显示什么内容
void ShowString(unsigned char x, unsigned char y, unsigned char *str)
{

        if (x == 1)
        {
                write_com(0x80 | y - 1);
        }
        if (x == 2)
        {
                write_com(0xc0 | y - 1);
        }
        //输出字符串

        while (*str != '\0')
        {
                write_date(*str);
                str++;
        }
}
void main(void)
{
        init();
        init_lcd();
        ShowString(1, 1, "test");
        while (1)
        {
                ShowString(2, 5, "test");
        };
}